import requests
import json
from common.utils import get_spell, get_abbreviation
from common.read_file import read_json
from common.write_file import write_json
from staffInfo.department.config import *

dictionary = read_json(DICTIONARY_PATH)


def get_data(cookie):
    cookies = {
        'PHPSESSID': cookie
    }
    r = requests.get(LOCAL_HOST + DEP_URL, cookies=cookies)
    rs = r.text
    rs = json.loads(rs)
    return rs
    pass


def save_date(cookie):
    data = get_data(cookie)
    dep_dict = {}
    dep_tree = {}
    for dep_id in data:
        tmp_dep_list = data[dep_id]
        dep_tree[dep_id] = list(tmp_dep_list.keys())
        for dep in tmp_dep_list:
            dep_dict[dep] = init_dep_unit(tmp_dep_list[dep], dep)
        pass
    results = {
        'dep_dict': dep_dict,
        'dep_tree': dep_tree,
    }
    write_json(results, DEPARTMENT_SAVE_PATH)
    pass


def init_dep_unit(dep_name, dep_id):
    return {
        'dep_name': dep_name,
        'dep_id': dep_id,
        'spell': get_spell(dep_name, dictionary),
        'abbreviation': get_abbreviation(dep_name, dictionary),
    }
    pass


# test_cookie = 'ra7u61d5q1ismp3si4prjppkp2'
# save_date(test_cookie)
